package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.BrokerCustomBean;
import com.csweimei.rentmanagementtreasure.http.entity.BrokerDailyBean;
import com.csweimei.rentmanagementtreasure.http.entity.BrokerFollowupBean;
import com.csweimei.rentmanagementtreasure.http.entity.BrokerHousingBean;
import com.csweimei.rentmanagementtreasure.http.entity.BrokerInfoBean;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class StaffDetailVM extends TitleVM {
    public StaffDetailVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("名片");
        setRightImg(R.mipmap.more_white);
        setRightIconVisible(View.GONE);
        setTitleTextColor("white");
    }

    @Override
    protected void rightIconOnClick() {
        super.rightIconOnClick();



    }

    public ObservableField<String> userid = new ObservableField<>();
    public ObservableField<String> companyID = new ObservableField<>();

    public ObservableField<Integer> showHouse = new ObservableField<>(View.VISIBLE);
    public ObservableField<Integer> showCustomer = new ObservableField<>(View.GONE);
    public ObservableField<Integer> showDaily = new ObservableField<>(View.GONE);
    public ObservableField<Integer> showFollow = new ObservableField<>(View.GONE);

    public void recyComm(){
        showHouse.set(View.GONE);
        showCustomer.set(View.GONE);
        showDaily.set(View.GONE);
        showFollow.set(View.GONE);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        getBaseInfo();
    }

    public ObservableField<BrokerInfoBean.DataDTO> baseInfo = new ObservableField<>();
    private void getBaseInfo() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",userid.get());
        map.put("companyID",companyID.get());
        model.brokeruser(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BrokerInfoBean>() {
                    @Override
                    public void onNext(BrokerInfoBean o) {

                        if (o.issucc){
                            baseInfo.set(o.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }



    public ObservableList<BHouseItemVM> houseList = new ObservableArrayList<>();
    public ItemBinding<BHouseItemVM> houseIB = ItemBinding.of(BR.viewModel,R.layout.item_house_b);
    public void brokerHousingData(){
        Map<String,String> map = new HashMap<>();
        map.put("userid",userid.get());
        map.put("companyID",companyID.get());
        model.brokerhousingdata(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BrokerHousingBean>() {
                    @Override
                    public void onNext(BrokerHousingBean o) {
                        if (o.issucc){
                            houseList.clear();
                            for (BrokerHousingBean.DataDTO house : o.getData()) {
                                BHouseItemVM houseItemVM = new BHouseItemVM(StaffDetailVM.this);
                                houseItemVM.setData(house);
                                houseList.add(houseItemVM);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableList<BCustomItemVM> customList = new ObservableArrayList<>();
    public ItemBinding<BCustomItemVM>  customIB = ItemBinding.of(BR.viewModel,R.layout.item_customer_b);
    public void brokerCustomData(){
        Map<String,String> map = new HashMap<>();
        map.put("userid",userid.get());
        map.put("companyID",companyID.get());
        model.brokercustom(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BrokerCustomBean>() {
                    @Override
                    public void onNext(BrokerCustomBean o) {
                        if (o.issucc){
                            customList.clear();
                            for (BrokerCustomBean.DataDTO custom : o.getData()) {
                                BCustomItemVM bCustomItemVM = new BCustomItemVM(titleVM);
                                bCustomItemVM.setData(custom);
                                customList.add(bCustomItemVM);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableList<BDailyItemVM> dailyList = new ObservableArrayList<>();
    public ItemBinding<BDailyItemVM>  dailyIB = ItemBinding.of(BR.viewModel,R.layout.item_dp_b);
    public void brokerDailyData(){
        Map<String,String> map = new HashMap<>();
        map.put("userid",userid.get());
        map.put("companyID",companyID.get());
        model.brokerdaily(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BrokerDailyBean>() {
                    @Override
                    public void onNext(BrokerDailyBean o) {
                        if (o.issucc){
                            dailyList.clear();
                            for (BrokerDailyBean.DataDTO daily : o.getData()) {
                                BDailyItemVM bDailyItemVM = new BDailyItemVM(titleVM);
                                bDailyItemVM.setData(daily);
                                dailyList.add(bDailyItemVM);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableList<BFollowItemVM> followList = new ObservableArrayList<>();
    public ItemBinding<BFollowItemVM>  followIB = ItemBinding.of(BR.viewModel,R.layout.item_follow_b);
    public void brokerFollowupData(){
        Map<String,String> map = new HashMap<>();
        map.put("userid",userid.get());
        map.put("companyID",companyID.get());
        model.brokerfollowup(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BrokerFollowupBean>() {
                    @Override
                    public void onNext(BrokerFollowupBean o) {
                        if (o.issucc){
                            followList.clear();
                            for (BrokerFollowupBean.DataDTO follow : o.getData()) {
                                BFollowItemVM bFollowItemVM = new BFollowItemVM(titleVM);
                                bFollowItemVM.setData(follow);
                                followList.add(bFollowItemVM);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
